app.controller("addupdatep",function($scope,$http, $stateParams,get,post,$rootScope,Md5,$state){
	$scope.sexList=[{id:-1,name:"请选择"},{id:0,name:"是"},{id:1,name:'否'}]
	//取消
	$scope.rement=function(){
		$state.go("home."+$stateParams.url,{page:$stateParams.page},{
						     		reload:true
						     	})
	}
	$scope.selectss=function(){
		
		//项目类型
		get.list("dictionary/projectTypeDic").then(function(e){
			
				$scope.projectTypeDic = e
				$scope.projectTypeDic.unshift({
						diName:"请选择",
						diId:-1
					})
				$scope.add.pType=$scope.projectTypeDic[0]
			})
		
		//销售负责人
		get.list("dictionary/staffSale").then(function(e){
			
				$scope.staffsale = e
				$scope.staffsale.unshift({
						diName:"请选择",
						diId:-1
					})
				
			})
		
		//部门
		get.list("department/all").then(function(e){
				$scope.department = e
		})
		
		
	}
	$scope.selectss();
	$scope.isshows=false
			//所属部门
			$scope.did={
						value:{
							diName:"请选择",
							diId:null
						}
					}
			//项目类型
			$scope.pType={
						value:{
							diName:"请选择",
							diId:null
						}
					}
			//项目状态
			$scope.pStatus={
						value:{
							diName:"请选择",
							diId:null
						}
					}
			//销售负责人
			$scope.salers={
						value:{
							diName:"请选择",
							diId:null
						}
					}
	$scope.add={};
	setTimeout(function (){
	if($stateParams.id!=''){
		get.list('project/findOne',{pId:$stateParams.id}).then(function(e){
							$scope.pickertime=e.pStartTime||''
							$scope.pickertimes=e.pEndTime||''
							$scope.add.did=e.departmentId
							$scope.add.pSerialNum=e.pSerialNum
							$scope.add.pName=e.pName
							$scope.add.address=e.address
							$scope.add.departmentId=e.departmentId
							$scope.add.pType=e.pType||{
														diName:"请选择",
														diId:-1
													}
							$scope.add.pStatus=e.pStatus||-1
							$scope.add.pResponsible=e.pResponsible||''
							$scope.add.pResponsibleContact=e.pResponsibleContact||''
							$scope.add.pSaler=e.pSaler
							$scope.add.startTime=e.pStartTime||''
							$scope.add.entryTime=e.pStartTime||''
							$scope.add.pEndTime=e.pEndTime||''
							$scope.add.pBreakUp=e.pBreakUp||-1
							$scope.add.pRemark=e.pRemark||''
					//所属部门
					angular.forEach($scope.department,function(o){
						if(e.departmentId==o.diId){
							$scope.did={
								value:o
							}
						}
					})
					//销售负责人
					angular.forEach($scope.staffsale,function(o){
						if(e.pSaler==o.diId){
							$scope.salers={
								value:o
							}
						}
					})
					
					//工作状态
					angular.forEach($scope.projectTypeDic,function(o){
						if(e.pType==o.diId){
							/*$scope.pType={
									value:o
							}*/
						$scope.add.pType=o
						}
					})
		})
	}else{
		
		
		$scope.add.pSerialNum="WXKJXM"+new Date().format("yyyyMMdd")
			$scope.add.pStatus=-1
			$scope.add.pBreakUp=-1
		$scope.add.pEndTime=''
	}
	},200)
	   $scope.$watch('add.pType', function (value) {
//	   	if (newValue === oldValue)return;
	   	if(null!=value&&null!=value.diId){
			get.list("dictionary/projectStateDic",{diCode:value.diCode,diName:value.diName}).then(function(date){
					$scope.projectStateDic = date
					$scope.projectStateDic.unshift({
						diName:"请选择",
						diId:-1
					})
					//$scope.add.pStatus=-1
					angular.forEach($scope.projectStateDic,function(o){
								if($scope.add.pStatus==o.diId){
										$scope.pStatus={
												value:o
										}
								
									}
								})
					})
			}
	   })
	
	
	$scope.sub=function(val){
		$scope.add.startTime=$scope.add.entryTime||''
		$scope.add.pEndTime=$scope.add.working||$scope.add.pEndTime
		if($scope.add.pSaler!=null){
			$scope.add.pSaler=$scope.add.pSaler.diId||$scope.add.pSaler
		}else{
			$scope.add.pSaler=""
		}
		
		$scope.add.pType=$scope.add.pType.diId||$scope.add.pType
		$scope.add.departmentId=$scope.did.value.diId||$scope.add.departmentId
			if($stateParams.id!=''){
				$scope.add.pId=$stateParams.id
				post.list("project/update",$scope.add).then(function(o){
						$state.go("home."+$stateParams.url,{page:$stateParams.page},{
						     		reload:true
						     	})
				})
			}else{
				
				post.list("project/add",$scope.add).then(function(o){
						$state.go("home."+$stateParams.url,{page:$stateParams.page},{
						     		reload:true
						     	})
				})
				
			}
			
			
		
		
	}
})
